package com.encyclopedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.encyclopedia.entity.Genus;
import com.encyclopedia.mapper.GenusMapper;
import com.encyclopedia.result.exception.ServiceException;
import com.encyclopedia.result.exenum.RequestException;
import com.encyclopedia.service.GenusService;
import com.encyclopedia.service.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

//属目
@Service
public class GenusServiceImpl implements GenusService {
    @Autowired
    private GenusMapper genusMapper;
    @Autowired
    private RedisUtils redisUtils;

    /**
     * 根据科目id查询属目
     *
     * @param familyId
     * @return
     */
    @Override
    public List<Genus> getGenusAllByFamilyId(Integer familyId) {

        List<Genus> genusList = genusMapper.selectList(new LambdaQueryWrapper<Genus>().eq(Genus::getFamilyId, familyId));
        if (genusList == null) {
            throw new ServiceException(RequestException.NOT_FOUND);
        }
        return genusList;
    }

    /**
     * 根据id或属目名称查询属目
     *
     * @param genusId
     * @return
     */
    @Override
    public Genus getGenusOneById(Integer genusId) {
        if (genusId == null) throw new ServiceException();
        String key = "GenusServiceImplById:" + genusId;
        Genus genusRedis = (Genus) redisUtils.get(key);
        if (genusRedis != null) return genusRedis;
        Genus genus = genusMapper.selectById(genusId);
        redisUtils.set(key, genus, 60 * 60 * 12);
        if (genus == null) throw new ServiceException(RequestException.NOT_FOUND);
        return genus;
    }

    @Override
    public Genus getGenusOneByName(String genusName) {
        if (genusName == null) throw new ServiceException();
        String key = "GenusServiceImplByName:" + genusName;
        Genus genusRedis = (Genus) redisUtils.get(key);
        if (genusRedis != null) return genusRedis;
        Genus genus = genusMapper.selectOne(new LambdaQueryWrapper<Genus>().eq(Genus::getGenusName, genusName));
        redisUtils.set(key, genus, 60 * 60 * 12);
        if (genus == null) throw new ServiceException(RequestException.NOT_FOUND);
        return genus;
    }

    @Override
    public List<Genus> getGenusByName(String genusName) {
        String key = "GenusServiceImplByName:" + genusName;
        if (genusName == null) throw new ServiceException(RequestException.NOT_FOUND);
        List<Genus> geners = (List<Genus>) redisUtils.get(key);
        if (geners != null) return geners;
        List<Genus> genusList = genusMapper.selectList(new LambdaQueryWrapper<Genus>().like(Genus::getGenusName, genusName));
        redisUtils.set(key, genusList, 60 * 60 * 12);
        if (genusList == null || genusList.isEmpty()) throw new ServiceException(RequestException.NOT_FOUND);
        return genusList;
    }


    /**
     * 添加属目
     *
     * @param genus
     * @return
     */
    @Override
    public Integer addGenus(Genus genus) {
        Genus genusOne = genusMapper.selectOne(new LambdaQueryWrapper<Genus>().eq(Genus::getGenusId, genus.getGenusId()));
        if (genusOne != null) {
            throw new ServiceException(RequestException.DATA_EXISTS_ERROR);
        } else {
            int insert = genusMapper.insert(genus);
            return genusOne.getGenusId();
        }


    }

    /**
     * 修改属目
     *
     * @param genus
     * @return
     */
    @Override
    public Integer updateGenus(Genus genus) {

        int i = genusMapper.updateById(genus);
        if (i == 0) {
            throw new ServiceException(RequestException.UPDATE_ERROR);
        }
        return i;
    }

    /**
     * 删除属目
     *
     * @param GenusId
     * @return
     */
    @Override
    public Integer deleteGenus(Integer GenusId) {
        int i = genusMapper.deleteById(GenusId);
        if (i == 0) {
            throw new ServiceException(RequestException.DELETE_ERROR);
        }
        return i;
    }


}
